package xin.tianhui.cloud.weblog.service.impl;

import xin.tianhui.cloud.weblog.model.dto.ArticleTxtResultDTO;
import xin.tianhui.cloud.weblog.model.po.ArticleTxt;
import xin.tianhui.cloud.weblog.mapper.ArticleTxtMapper;
import xin.tianhui.cloud.weblog.service.IArticleTxtService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Optional;

import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 文章内容 服务实现类
 * </p>
 *
 * @author junchen
 * @since 2018-04-08
 */
@Service
@CacheConfig(cacheNames="article_txt",keyGenerator="keyGenerator")
public class ArticleTxtServiceImpl extends ServiceImpl<ArticleTxtMapper, ArticleTxt> implements IArticleTxtService {

	@Cacheable
	@Override
	public ArticleTxtResultDTO getArticleTxt(Integer articleId) {
		ArticleTxt txt = new ArticleTxt();
		txt.setArticleId(articleId);
		QueryWrapper<ArticleTxt> wrapper = new QueryWrapper<>(txt);
		return Optional.ofNullable(baseMapper.selectOne(wrapper)).map(r->{
			ArticleTxtResultDTO dto = new ArticleTxtResultDTO();
			dto.setHtml(r.getHtml());
			dto.setMarkdown(r.getMarkdown());
			return dto;
		}).orElse(null);
	}

}
